package com.zxkj.commonpatrol.ui;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.czy.basecommon.utils.base.BaseVmDataBindingActivity;
import com.czy.basecommon.utils.base.PermissionsUtils;
import com.czy.basecommon.utils.base.StateData;
import com.czy.basecommon.utils.entity.event.SearchData;
import com.czy.basecommon.utils.tools.ActivityTool;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.luck.picture.lib.PhotoEditorActivity;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.model.EditPicEvent;
import com.permissionx.guolindev.PermissionX;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.GravityEnum;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.toast.XToast;
import com.zxkj.commonpatrol.BR;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.adapt.MyMediaAdapt;
import com.zxkj.commonpatrol.adapt.SubHistoryQuestAdapt;
import com.zxkj.commonpatrol.databinding.ActivityAddExceptionBinding;
import com.zxkj.commonpatrol.model.entity.AlarmInfoEntrty;
import com.zxkj.commonpatrol.model.entity.Group;
import com.zxkj.commonpatrol.model.entity.GroupMember;
import com.zxkj.commonpatrol.model.entity.MyMediaEntry;
import com.zxkj.commonpatrol.model.entity.SubAlarmLogHistory;
import com.zxkj.commonpatrol.model.entity.SubAlarmLogHistorys;
import com.zxkj.commonpatrol.model.entity.itemPicEntity;
import com.zxkj.commonpatrol.model.entity.itemVideoEntity;
import com.zxkj.commonpatrol.model.entity.itemVioceEntity;
import com.zxkj.commonpatrol.model.response.AlarmIdBean;
import com.zxkj.commonpatrol.utils.AudioRecorderButton;
import com.zxkj.commonpatrol.utils.VideoRecordActivity;
import com.zxkj.commonpatrol.viewmodel.AddExceptionViewModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class AddExceptionActivity extends BaseVmDataBindingActivity<ActivityAddExceptionBinding, AddExceptionViewModel> {
    private MyMediaAdapt myMediaAdapt1;
    private MyMediaAdapt myMediaAdapt2;
    private MyMediaAdapt myMediaAdapt3;
    private static final int REQUEST_VIDEO = 99;
    private static final int REQUEST_VIDEO2 = 100;
    private static final int REQUEST_VIDEO3 = 101;

    private List<LocalMedia> selectListData1 = new ArrayList<>();//group1
    private List<LocalMedia> selectListData2 = new ArrayList<>();//group2
    private List<LocalMedia> selectListData3 = new ArrayList<>();//group3
    private String logId;
    private String AlarmId;
    private MiniLoadingDialog loadingDialog;
    private String tips;
    private int mPhotoSize=0;
    private int IsUnCheck=0;//0  正常添加异常,1是 修复进来的
    private SubHistoryQuestAdapt adapt;
    private boolean js=false;
    private int capturePhoto;
    private int Irregular;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_add_exception;
    }

    @Override
    public int initVariableId() {
        return BR.AddExceptionViewModel;
    }


    @Override
    protected void initUI() {

        logId = getIntent().getStringExtra("LogId");
        tips = getIntent().getStringExtra("tips");
        String title = getIntent().getStringExtra("title");
         js = getIntent().getBooleanExtra("js", false);
        if (getIntent().hasExtra("IsUnCheck")){
            IsUnCheck=getIntent().getIntExtra("IsUnCheck",0);
        }
         if (TextUtils.isEmpty(tips)){
             binding.llHead.setVisibility(View.GONE);
         }else{
             binding.llHead.setVisibility(View.VISIBLE);
             binding.tvHead.setText(tips);
         }
         capturePhoto = getIntent().getIntExtra("CapturePhoto", 0);
         Irregular=getIntent().getIntExtra("Irregular",0);


        EventBus.getDefault().register(this);
        //初始化recyleview
        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
        binding.recyGroup1Pic.setLayoutManager(layoutManager1);

        LinearLayoutManager layoutManager2 = new LinearLayoutManager(this);
        layoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);
        binding.recyGroup2Pic.setLayoutManager(layoutManager2);


        LinearLayoutManager layoutManager3 = new LinearLayoutManager(this);
        layoutManager3.setOrientation(LinearLayoutManager.HORIZONTAL);
        binding.recyGroup3Pic.setLayoutManager(layoutManager3);




        myMediaAdapt1 = new MyMediaAdapt(getSupportFragmentManager(), 1, this);//第一組
        binding.recyGroup1Pic.setAdapter(myMediaAdapt1);


        myMediaAdapt2 = new MyMediaAdapt(getSupportFragmentManager(), 2, this);//第二組
        binding.recyGroup2Pic.setAdapter(myMediaAdapt2);


        myMediaAdapt3 = new MyMediaAdapt(getSupportFragmentManager(), 3, this);//第三組
        binding.recyGroup3Pic.setAdapter(myMediaAdapt3);
        binding.llGroup2.setVisibility(View.GONE);
        binding.llGroup3.setVisibility(View.GONE);
        binding.recy.setLayoutManager(new LinearLayoutManager(this));
        if (IsUnCheck==0){
            binding.llIsUncheck.setVisibility(View.GONE);
        }else{
            binding.llIsUncheck.setVisibility(View.VISIBLE);
            if (js){
                binding.llJs.setVisibility(View.GONE);
                binding.tvTitle.setText(title+"-"+"檢視");
                getInitData();
            }else {
                binding.llJs.setVisibility(View.VISIBLE);
                binding.tvTitle.setText(title+"-"+"未解決");
                getInitData();
            }
          ;
        }
        if (capturePhoto==1){
            binding.btnNo.setBackgroundColor(getColor(R.color.gray));
            binding.btnNo.setClickable(false);
            binding.btnNo.setEnabled(false);
        }
        if (capturePhoto==1){
            if (Irregular==0){
                viewModel.descr.set("完成拍照");
                binding.tvTitle.setText("新增備註");
                binding.textView9.setText("備註描述:");
            }



        }

        boolean b = PermissionsUtils.from(this).setPermissions(PermissionsUtils.permissionsRecord);
        Log.e("audio",String.valueOf(b));

    }

    private void getInitData() {
        if (loadingDialog == null) {
            loadingDialog = new MiniLoadingDialog(this, "加載中...");
        }
        loadingDialog.show();
        //获取历史详情
        viewModel.getHistroy(logId);
    }

    @Override
    protected void initData() {


        }
//        if (XXPermissions.hasPermission(AddExceptionActivity.this, Permission.RECORD_AUDIO)) {
//            XToast.error(AddExceptionActivity.this,"获取到权限或者部分权限").show();
////            XXPermissions.startPermissionActivity(AddExceptionActivity.this);
//        }else {
//            XToast.error(AddExceptionActivity.this,"还没有获取到权限或者部分权限未授予").show();
//        }


    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void setListener() {
        super.setListener();
        //第一组语音回调
        binding.addVo.setAudioFinishRecorderListener((seconds, filePath) -> {
            myMediaAdapt1.addData(new MyMediaEntry(2, new itemVioceEntity(0, filePath), null, null));
            long counts = myMediaAdapt1.getData().stream().filter(p -> p.getVioceEntity() != null).map(p -> p.getVioceEntity()).count();
            int count = Integer.parseInt(String.valueOf(counts));
            binding.addVo.setCountss(count);
        });
        //第二组语音回调
        binding.addVo2.setAudioFinishRecorderListener((seconds, filePath) -> {
            myMediaAdapt2.addData(new MyMediaEntry(2, new itemVioceEntity(0, filePath), null, null));
            long counts = myMediaAdapt2.getData().stream().filter(p -> p.getVioceEntity() != null).map(p -> p.getVioceEntity()).count();
            int count = Integer.parseInt(String.valueOf(counts));
            binding.addVo2.setCountss(count);
        });
        //第三组语音回调
        binding.addVo3.setAudioFinishRecorderListener((seconds, filePath) -> {
            myMediaAdapt3.addData(new MyMediaEntry(2, new itemVioceEntity(0, filePath), null, null));
            long counts = myMediaAdapt3.getData().stream().filter(p -> p.getVioceEntity() != null).map(p -> p.getVioceEntity()).count();
            int count = Integer.parseInt(String.valueOf(counts));
            binding.addVo3.setCountss(count);
        });


        binding.ivAddPo.setOnClickListener(v -> {
            addPic(myMediaAdapt1, PictureConfig.CHOOSE_REQUEST1);

        });
        binding.ivAddPo2.setOnClickListener(v -> {
            addPic(myMediaAdapt2, PictureConfig.CHOOSE_REQUEST2);

        });
        binding.ivAddPo3.setOnClickListener(v -> {
            addPic(myMediaAdapt3, PictureConfig.CHOOSE_REQUEST3);

        });
        binding.addVe.setOnClickListener(v -> {
            addVideo(myMediaAdapt1, REQUEST_VIDEO);

        });
        binding.addVe2.setOnClickListener(v -> {
            addVideo(myMediaAdapt2, REQUEST_VIDEO2);


        });

        binding.addVe3.setOnClickListener(v -> {
            addVideo(myMediaAdapt3, REQUEST_VIDEO3);


        });

        binding.ivDeleteGroup2.setOnClickListener(v -> {
            binding.llGroup2.setVisibility(View.GONE);
            binding.ivAddGroup.setVisibility(View.VISIBLE);
            myMediaAdapt2.getData().clear();
            myMediaAdapt2.MyNotifyDataSetChanged();


        });

        binding.ivDeleteGroup3.setOnClickListener(v -> {
            binding.ivAddGroup.setVisibility(View.VISIBLE);
            binding.llGroup3.setVisibility(View.GONE);
            myMediaAdapt3.getData().clear();
            myMediaAdapt3.MyNotifyDataSetChanged();


        });

        binding.ivAddGroup.setOnClickListener(v -> {
            if (binding.llGroup3.getVisibility() == View.VISIBLE && binding.llGroup2.getVisibility() == View.GONE) {
                binding.llGroup2.setVisibility(View.VISIBLE);
                binding.ivAddGroup.setVisibility(View.GONE);
            } else if (binding.llGroup3.getVisibility() == View.GONE && binding.llGroup2.getVisibility() == View.VISIBLE) {
                binding.llGroup3.setVisibility(View.VISIBLE);
                binding.ivAddGroup.setVisibility(View.GONE);
            } else if (binding.llGroup3.getVisibility() == View.GONE && binding.llGroup2.getVisibility() == View.GONE) {
                binding.llGroup2.setVisibility(View.VISIBLE);
            }

        });


        binding.btnNo.setOnClickListener(v -> {
            if (loadingDialog == null) {
                loadingDialog = new MiniLoadingDialog(this, "加載中...");
            }
            loadingDialog.show();
            if (TextUtils.isEmpty(binding.edPointQuestList.getText().toString().trim())){
                XToast.error(AddExceptionActivity.this, "請填寫描述").show();
                if (loadingDialog!=null){
                    loadingDialog.dismiss();
                }

                return;
            }

            if(IsUnCheck==0){
                //上傳巡檢記錄
                viewModel.uploadAlarmLog(logId,IsUnCheck);
            }else{
                 //上傳未修复巡檢記錄
                viewModel.uploadAlarmLog(logId,IsUnCheck);
            }





        });

        binding.qkBack.setOnClickListener(v->{



            if (IsUnCheck==0){
                new MaterialDialog.Builder(AddExceptionActivity.this)
                        .content("是否確認離開?")
                        .contentGravity(GravityEnum.CENTER)
                        .negativeColor(getResources().getColor(R.color.colorPrimary))
                        .positiveColor(getResources().getColor(R.color.colorPrimary))
                        .buttonsGravity(GravityEnum.CENTER)
                        .positiveText("是")
                        .negativeText("否")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                //發消息
                                LiveEventBus.get("EventDeciveList").post("1");
                                ActivityTool.getInstance().popOneActivity(AddExceptionActivity.this);
                            }
                        }).show();

            }else{
                if (js){
                    ActivityTool.getInstance().popOneActivity(AddExceptionActivity.this);
                }else{
                    new MaterialDialog.Builder(AddExceptionActivity.this)
                            .content("是否確認離開?")
                            .contentGravity(GravityEnum.CENTER)
                            .negativeColor(getResources().getColor(R.color.colorPrimary))
                            .positiveColor(getResources().getColor(R.color.colorPrimary))
                            .buttonsGravity(GravityEnum.CENTER)
                            .positiveText("是")
                            .negativeText("否")
                            .onPositive(new MaterialDialog.SingleButtonCallback() {
                                @Override
                                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                    LiveEventBus.get("refershQuestDeatil").post(true);
                                    ActivityTool.getInstance().popOneActivity(AddExceptionActivity.this);
                                }
                            }).show();
                }

            }

        });
    }

    @Override
    protected void dataObserver() {
        super.dataObserver();
        LiveEventBus.get("diss", Boolean.class).observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    binding.scrollView.requestDisallowInterceptTouchEvent(false);
                } else {
                    binding.scrollView.requestDisallowInterceptTouchEvent(true);
                }
            }
        });

        viewModel.AlarmId.observe(this, new Observer<StateData<String>>() {
            @Override
            public void onChanged(StateData<String> alarmIdBeanStateData) {

                if (alarmIdBeanStateData.getStatus() == StateData.DataStatus.SUCCESS) {
                    if (alarmIdBeanStateData.getData() != null) {
                        AlarmId = alarmIdBeanStateData.getData();
                        UploadAll();
                    }
                }else {
                    if ( loadingDialog!=null){
                        loadingDialog.dismiss();
                    }
                }

            }
        });

        viewModel.mData.observe(this, new Observer<StateData<SubAlarmLogHistorys>>() {
            @Override
            public void onChanged(StateData<SubAlarmLogHistorys> listStateData) {
                if (listStateData.getStatus()== StateData.DataStatus.SUCCESS){
                    if ( loadingDialog!=null){
                        loadingDialog.dismiss();
                    }
                    if (adapt==null){
                        if (listStateData.getData().getSubAlarmInfo()!=null){

                            adapt =new SubHistoryQuestAdapt(R.layout.item_quest_detail_list,listStateData.getData().getSubAlarmInfo());

                            if (listStateData.getData().getAlarmInfo()!=null){
                                if (!TextUtils.isEmpty(listStateData.getData().getAlarmInfo().getResultDesc())){
                                    SubAlarmLogHistory alarmInfo = listStateData.getData().getAlarmInfo();
                                    alarmInfo.setType(2);
                                    adapt.addData(alarmInfo);
                                }



                            }
                        }

                        binding.recy.setAdapter(adapt);
                    }else{
                        if (listStateData.getData().getSubAlarmInfo()!=null){
                            adapt.setNewData(listStateData.getData().getSubAlarmInfo());
                            if (listStateData.getData().getAlarmInfo()!=null){


                                if (!TextUtils.isEmpty(listStateData.getData().getAlarmInfo().getResultDesc())){
                                    SubAlarmLogHistory alarmInfo = listStateData.getData().getAlarmInfo();
                                    alarmInfo.setType(2);
                                    adapt.addData(alarmInfo);
                                }


                            }
                        }

                    }

                    adapt.setOnCallPhoneCallBack(new SubHistoryQuestAdapt.OnCallPhoneCallBack() {
                        @Override
                        public void onCallPhone(String number) {
                            if (!TextUtils.isEmpty(number)) {
                                callPhone(number);
                            }
                        }
                    });
                    adapt.setmDataCallBack(new SubHistoryQuestAdapt.MyCheckPointListCallBack<Integer>() {
                        @Override
                        public void onDataSuccessCallBack(Integer s) {
                            if (loadingDialog == null) {
                                loadingDialog = new MiniLoadingDialog(AddExceptionActivity.this, "加載中...");
                            }
                            loadingDialog.show();
                            //去修復
                            viewModel.Check(s+"");
                        }

                        @Override
                        public void onDataFailCallBack(Integer s, Integer t1) {

                        }
                    });


                }else{
                    if ( loadingDialog!=null){
                        loadingDialog.dismiss();
                    }
                }
            }
        });
        viewModel.Checked.observe(this, new Observer<StateData<Object>>() {
            @Override
            public void onChanged(StateData<Object> objectStateData) {
                if (loadingDialog == null) {
                    loadingDialog.dismiss();
                }
               if (objectStateData.getStatus()== StateData.DataStatus.SUCCESS){
                   getInitData();
               }else{

               }
            }
        });

        LiveEventBus.get("ff",Boolean.class).observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (loadingDialog!=null){
                    loadingDialog.dismiss();
                }
                if (IsUnCheck==0){
                    LiveEventBus.get("refershQuestDeatil").post(true);
                    ActivityTool.getInstance().popOneActivity(AddExceptionActivity.this);
                }else{
//                    viewModel.descr.set("");
//                    myMediaAdapt1.getData().clear();
//
//                    myMediaAdapt2.getData().clear();
//                    myMediaAdapt3.getData().clear();
//                    myMediaAdapt1.notifyDataSetChanged();
//                    myMediaAdapt2.notifyDataSetChanged();
//                    myMediaAdapt3.notifyDataSetChanged();
//                    binding.llGroup2.setVisibility(View.GONE);
//                    binding.llGroup3.setVisibility(View.GONE);
//                    getInitData();
                    LiveEventBus.get("refershQuestDeatil").post(true);
                    ActivityTool.getInstance().popOneActivity(AddExceptionActivity.this);
                }


            }
        });

         LiveEventBus.get("mPhotoSize",Integer.class).observe(this, new Observer<Integer>() {
             @Override
             public void onChanged(Integer integer) {
                  if (integer>0){
                      binding.btnNo.setBackgroundColor(getColor(R.color.colorPrimary));
                      binding.btnNo.setClickable(true);
                      binding.btnNo.setEnabled(true);
                  }
             }
         });
        LiveEventBus.get("mPhotoSizes",Integer.class).observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                mPhotoSize=mPhotoSize-integer;
                 if (mPhotoSize<=0){
                     if (capturePhoto==1){
                         binding.btnNo.setBackgroundColor(getColor(R.color.gray));
                         binding.btnNo.setClickable(false);
                         binding.btnNo.setEnabled(false);

                     }


                 }
            }
        });

    }

    private void callPhone(String number) {
        new MaterialDialog.Builder(this)
                .content("是否撥打"+number+"?")
                .contentGravity(GravityEnum.CENTER)
                .negativeColor(getResources().getColor(R.color.colorPrimary))
                .positiveColor(getResources().getColor(R.color.colorPrimary))
                .buttonsGravity(GravityEnum.CENTER)
                .positiveText("是")
                .negativeText("否")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                            ActivityTool.getInstance().finishAllActivities();
//                          ARouter.getInstance().build("/Login/LoginActivity").navigation(getContext().getApplicationContext());
                        Intent intent = new Intent(Intent.ACTION_CALL);
                        Uri data = Uri.parse("tel:" + number);
                        intent.setData(data);
                        startActivity(intent);

                    }
                }).show();

    }


    public void UploadAll(){
        //上傳圖片和上傳語音 和上傳視頻
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<itemPicEntity> pic = myMediaAdapt1.getPic();
                List<itemPicEntity> pic1 = myMediaAdapt2.getPic();
                List<itemPicEntity> pic2 = myMediaAdapt3.getPic();
                List<itemVioceEntity> vioce = myMediaAdapt1.getVioce();
                List<itemVioceEntity> vioce1 = myMediaAdapt2.getVioce();
                List<itemVioceEntity> vioce2 = myMediaAdapt3.getVioce();
                if (pic != null && pic.size() > 0) {

                    for (int i = 0; i < pic.size(); i++) {
                        //上傳第一組
                        viewModel.uploadPhoto(pic.get(i).getPicPath(), AlarmId, 1, i + 1,IsUnCheck);
                    }

                }
                if (pic1 != null && pic1.size() > 0) {

                    for (int i = 0; i < pic1.size(); i++) {
                        //上傳第二組
                        viewModel.uploadPhoto(pic1.get(i).getPicPath(), AlarmId, 2, i + 1,IsUnCheck);
                    }

                }
                if (pic2 != null && pic2.size() > 0) {

                    for (int i = 0; i < pic2.size(); i++) {
                        //上傳第三組
                        viewModel.uploadPhoto(pic2.get(i).getPicPath(), AlarmId, 3, i + 1,IsUnCheck);
                    }

                }


                if (vioce != null && vioce.size() > 0) {
                    //上傳第一組語音


                    for (int i = 0; i < vioce.size(); i++) {
                        viewModel.uploadVoice(vioce.get(i).getViocePath(), AlarmId, 1, i + 1,IsUnCheck);
                    }

                }
                if (vioce1 != null && vioce1.size() > 0) {
                    //上傳第一組語音


                    for (int i = 0; i < vioce1.size(); i++) {
                        viewModel.uploadVoice(vioce1.get(i).getViocePath(), AlarmId, 2, i + 1,IsUnCheck);
                    }

                }
                if (vioce2 != null && vioce2.size() > 0) {
                    //上傳第一組語音


                    for (int i = 0; i < vioce2.size(); i++) {
                        viewModel.uploadVoice(vioce2.get(i).getViocePath(), AlarmId, 3, i + 1,IsUnCheck);
                    }

                }


                LiveEventBus.get("ff").post(true);


            }
        }).start();
    }


    private void setImage(int x) {

        PictureSelector.create(AddExceptionActivity.this)// 是否可预览图片 true or false
                .openCamera(PictureMimeType.ofImage())
                .isCompress(true)
                .compressQuality(50)
                .forResult(x);

    }
    @Override
    public void onBackPressed() {
        if (IsUnCheck==0){
            new MaterialDialog.Builder(AddExceptionActivity.this)
                    .content("是否確認離開?")
                    .contentGravity(GravityEnum.CENTER)
                    .negativeColor(getResources().getColor(R.color.colorPrimary))
                    .positiveColor(getResources().getColor(R.color.colorPrimary))
                    .buttonsGravity(GravityEnum.CENTER)
                    .positiveText("是")
                    .negativeText("否")
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            //發消息
                            LiveEventBus.get("EventDeciveList").post("1");
                            ActivityTool.getInstance().popOneActivity(AddExceptionActivity.this);
                        }
                    }).show();
        }else{
            if (js){
                ActivityTool.getInstance().popOneActivity(AddExceptionActivity.this);
            }else{
                new MaterialDialog.Builder(AddExceptionActivity.this)
                        .content("是否確認離開?")
                        .contentGravity(GravityEnum.CENTER)
                        .negativeColor(getResources().getColor(R.color.colorPrimary))
                        .positiveColor(getResources().getColor(R.color.colorPrimary))
                        .buttonsGravity(GravityEnum.CENTER)
                        .positiveText("是")
                        .negativeText("否")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                LiveEventBus.get("refershQuestDeatil").post(true);
                                ActivityTool.getInstance().popOneActivity(AddExceptionActivity.this);
                            }
                        }).show();
            }

        }

    }

    public AudioRecorderButton getGroup1MyAudioRecorderButton() {

        return binding.addVo;

    }

    public AudioRecorderButton getGroup2MyAudioRecorderButton() {

        return binding.addVo2;

    }

    public AudioRecorderButton getGroup3MyAudioRecorderButton() {

        return binding.addVo3;

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST1:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    LocalMedia localMedia1 = selectList.get(0);
                    selectListData1.add(localMedia1);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true  注意：音视频除外
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true  注意：音视频除外
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    for (LocalMedia bean : selectList) {
                        boolean compressed = bean.isCompressed();
                        String compressPath = bean.getCompressPath();

//                        if (selectListData1.size() == 3) {
//
//                        } else {
//
//                        }

                        Intent intent = new Intent(AddExceptionActivity.this, PhotoEditorActivity.class);
                        intent.putExtra("paths", compressPath);
                        intent.putExtra("type", PictureConfig.CHOOSE_REQUEST1);
                        startActivity(intent);

                    }
                    break;
                case PictureConfig.CHOOSE_REQUEST2:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> selectList1 = PictureSelector.obtainMultipleResult(data);
                    LocalMedia localMedia2 = selectList1.get(0);
                    selectListData2.add(localMedia2);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true  注意：音视频除外
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true  注意：音视频除外
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    for (LocalMedia bean : selectList1) {
                        String compressPath = bean.getCompressPath();

//                        if (selectListData2.size() == 3) {
//
//                        } else {
//
//                        }

                        Intent intent = new Intent(AddExceptionActivity.this, PhotoEditorActivity.class);
                        intent.putExtra("paths", compressPath);
                        intent.putExtra("type", PictureConfig.CHOOSE_REQUEST2);
                        startActivity(intent);

                    }
                    break;
                case PictureConfig.CHOOSE_REQUEST3:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> selectList2 = PictureSelector.obtainMultipleResult(data);
                    LocalMedia localMedia3 = selectList2.get(0);
                    selectListData3.add(localMedia3);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true  注意：音视频除外
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true  注意：音视频除外
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    for (LocalMedia bean : selectList2) {
                        String compressPath = bean.getCompressPath();

//                        if (selectListData3.size() == 3) {
//
//                        } else {
//
//                        }

                        Intent intent = new Intent(AddExceptionActivity.this, PhotoEditorActivity.class);
                        intent.putExtra("paths", compressPath);
                        intent.putExtra("type", PictureConfig.CHOOSE_REQUEST3);
                        startActivity(intent);

                    }
                    break;
                case REQUEST_VIDEO:

                    String path = data.getStringExtra("path");
                    String imgPath = data.getStringExtra("imagePath");
                    int type = data.getIntExtra("type", -1);
                    if (type == 0) {
                        myMediaAdapt1.addData(new MyMediaEntry(3, null, null, new itemVideoEntity(path, imgPath)));
                    }
                    break;

                case REQUEST_VIDEO2:
                    String path2 = data.getStringExtra("path");
                    String imgPath2 = data.getStringExtra("imagePath");
                    int type2 = data.getIntExtra("type", -1);
                    if (type2 == 0) {
                        myMediaAdapt2.addData(new MyMediaEntry(3, null, null, new itemVideoEntity(path2, imgPath2)));
                    }
                    break;

                case REQUEST_VIDEO3:
                    String path3 = data.getStringExtra("path");
                    String imgPath3 = data.getStringExtra("imagePath");
                    int type3 = data.getIntExtra("type", -1);
                    if (type3 == 0) {
                        myMediaAdapt3.addData(new MyMediaEntry(3, null, null, new itemVideoEntity(path3, imgPath3)));
                    }
                    break;


            }
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
//        File file = new File(ETFConstant.VIOCEPATH);
//        FileHelper.deleteFile(file);
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getEditPic(EditPicEvent editEvent) {
        if (editEvent.isIsok()) {
            switch (editEvent.getIvType()) {
                case PictureConfig.CHOOSE_REQUEST1:
                    mPhotoSize=mPhotoSize+1;
                    myMediaAdapt1.addData(new MyMediaEntry(1, null, new itemPicEntity(editEvent.getPath()), null));
                     LiveEventBus.get("mPhotoSize").post(mPhotoSize);
                    break;
                case PictureConfig.CHOOSE_REQUEST2:
                    mPhotoSize=mPhotoSize+1;
                    myMediaAdapt2.addData(new MyMediaEntry(1, null, new itemPicEntity(editEvent.getPath()), null));
                    LiveEventBus.get("mPhotoSize").post(mPhotoSize);
                    break;
                case PictureConfig.CHOOSE_REQUEST3:
                    mPhotoSize=mPhotoSize+1;
                    myMediaAdapt3.addData(new MyMediaEntry(1, null, new itemPicEntity(editEvent.getPath()), null));
                    LiveEventBus.get("mPhotoSize").post(mPhotoSize);
                    break;
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void addVideo(MyMediaAdapt adapt, int i) {
        long counts = adapt.getData().stream().filter(p -> p.getItemVideoEntity() != null).map(p -> p.getItemVideoEntity()).count();
        int count = Integer.parseInt(String.valueOf(counts));
        if (count == 1) {
            XToast.error(AddExceptionActivity.this, "添加視頻超過最大數量").show();
        } else {
            //
            Intent intent = new Intent();
            intent.setClass(AddExceptionActivity.this, VideoRecordActivity.class);

            startActivityForResult(intent, i);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void addPic(MyMediaAdapt adapt, int i) {
        long counts = adapt.getData().stream().filter(p -> p.getItemPicEntity() != null).map(p -> p.getItemPicEntity()).count();
        int count = Integer.parseInt(String.valueOf(counts));
        if (count == 2) {
            XToast.error(AddExceptionActivity.this, "添加圖片超過最大數量").show();
        } else {
            setImage(i);

        }
    }
}
